/*
 *page 1.0   2017-07-30 by Hale
 */
(function(whole){
    var obj = {};
    obj.Page = function(box,options){
        obj.box = box;
        obj.defaults = {
                totalPages: options.totalPages||9,//总页数
                liNums: options.liNums  || 9,//分页的数字按钮数(建议取奇数)
                activeColor: options.activeColor  || 'orange' ,//默认颜色
                firstPage: options.firstPage  || '首页',//首页按钮名称
                lastPage: options.lastPage  || '末页',//末页按钮名称
                prv: options.prv  || '«',//前一页按钮名称
                next: options.next  || '»',//后一页按钮名称
                hasFirstPage: options.hasFirstPage  || true,//是否有首页按钮
                hasLastPage: options. hasLastPage || true,//是否有末页按钮
                hasPrv: options. hasPrv || true,//是否有前一页按钮
                hasNext: options. hasNext || true,//是否有后一页按钮
                callBack : options.callBack
        };
        obj.createPageDiv(box);
    };
    obj.styles = function(cont,w){
        cont.style.width = "30px";
        cont.style.height = "30px";
        cont.style.backgroundColor = "#FFF";
        if(w){
            cont.style.padding = "0px "+w;
        }
        cont.style.float = "left";
        if(cont.previousElementSibling){
            cont.style.marginLeft = "10px";
        }
        cont.style.textAlign = "center";
        cont.style.lineHeight = "28px";
        cont.style.color = "#666";
        cont.style.fontSize = "12px";
        cont.style.borderRadius = "5px";
        cont.style.cursor = "pointer";
        cont.style.border = "1px solid #666";
        cont.onmouseenter = function(){
            cont.style.backgroundColor = obj.defaults.activeColor;
            cont.style.color = "white";
        };
        cont.onmouseleave = function(){
            cont.style.backgroundColor = "#FFF";
            cont.style.color = "#666"
        }
    };
    obj.createPageDiv = function(box){
        box.style.display = "table";
        box.style.margin = "0 auto";
        var s = 1;//当前在第几页
        var l = obj.defaults.totalPages;//总页数
        var n = obj.defaults.liNums;//每页页数
        var arr = [];//存入每页的每一页
        if(obj.defaults.hasFirstPage){
            var div1 = document.createElement("div");
            box.appendChild(div1);
            obj.styles(div1,"5px");
            div1.textContent = obj.defaults.firstPage;
            div1.style.visibility = "hidden";
            div1.onclick = function(){
                if(s == 1){
                    return
                }
                s = 1;
                targetHandler(s-1)
            }
        }
        if(obj.defaults.hasPrv){
            var div2 = document.createElement("div");
            box.appendChild(div2);
            obj.styles(div2,"5px");
            div2.textContent = obj.defaults.prv;
            div2.onclick = function(){
                s--;
                if(s == 0){
                    s = 1;
                    return
                }
                targetHandler(s-1)
            }
        }
        var ul = document.createElement("ul");
        box.appendChild(ul);
        ul.style.float = "left";
        ul.style.listStyle = "none";
        ul.style.padding = 0;
        ul.style.margin = 0;
        ul.style.marginLeft = "10px";
        for(var i = 0;i<n;i++){
            var li = document.createElement("li");
            ul.appendChild(li);
            li.textContent = i+1;
            arr.push(li);
            (function(i,li){
                obj.styles(li);
                if(i == 0){
                    li.style.backgroundColor = obj.defaults.activeColor;
                    li.style.color = "white";
                    li.onmouseleave = null;
                    obj.defaults.callBack(i+1);
                }
            })(i,li)
        }
        if(obj.defaults.hasNext){
            var div3 = document.createElement("div");
            box.appendChild(div3);
            obj.styles(div3,"5px");
            div3.textContent = obj.defaults.next;
            div3.onclick = function(){
                s++;
                if(s == l+1){
                    s = l;
                    return
                }
                targetHandler(s-1)
            }
        }
        if(obj.defaults.hasLastPage){
            var div4 = document.createElement("div");
            box.appendChild(div4);
            obj.styles(div4,"5px");
            div4.textContent = obj.defaults.lastPage;
            div4.onclick = function(){
                if(s == l){
                    return;
                }
                s = l;
                targetHandler(s-1)
            }
        }
        for(var j = 0;j<arr.length;j++){
            (function(j){
                arr[j].onclick = function(){
                    s = Math.floor(arr[j].textContent);
                    targetHandler(s-1)
                }
            })(j)
        }
        function targetHandler(m){
            var nn = m;
            if(m == 0){
                for(var f = 0;f<arr.length;f++){
                    arr[f].textContent = f+1;
                }
            }
            if(m+1>Math.floor(n/2)){
                if(m<l-(n-Math.floor(n/2))){
                    nn = Math.floor(n/2);
                    for(var a = 0;a<arr.length;a++){
                        var b = Math.floor(arr[a].textContent);
                        if(a<Math.floor(n/2)){
                            arr[a].textContent = m+1-(Math.floor(n/2)-a);
                        }else if(a>Math.floor(n/2)){
                            arr[a].textContent = m+1+(a-Math.floor(n/2));
                        }else {
                            arr[a].textContent = m+1;
                        }
                    }
                }else {
                    nn = m-(l-(n-Math.floor(n/2)))+Math.floor(n/2);
                    for(var c = 0;c<arr.length;c++){
                        var d = Math.floor(arr[c].textContent);
                        if(Math.floor(arr[n-1].textContent) != l){
                            arr[c].textContent = l-(n-c-1);
                        }
                    }
                }
            }else{
                nn = m;
                for(var a = 0;a<arr.length;a++){
                    arr[a].textContent =a+1;
                }
            }
            for(var k = 0;k<arr.length;k++){
                if(Math.floor(arr[0].textContent)!=1){
                    if(obj.defaults.hasFirstPage){
                        obj.box.firstElementChild.style.visibility = "visible"
                    }
                }else {
                    if(obj.defaults.hasFirstPage){
                        obj.box.firstElementChild.style.visibility = "hidden"
                    }
                }
                if(Math.floor(arr[obj.defaults.liNums-1].textContent)!=obj.defaults.totalPages){
                    if(obj.defaults.hasLastPage){
                        obj.box.lastElementChild.style.visibility = "visible"
                    }
                }else {
                    if(obj.defaults.hasLastPage){
                        obj.box.lastElementChild.style.visibility = "hidden"
                    }
                }
                if(arr[k] == arr[nn]){
                    arr[k].style.backgroundColor = obj.defaults.activeColor;
                    arr[k].style.color = "white";
                    arr[k].onmouseleave = null;
                    obj.defaults.callBack(m+1);
                }else {
                    arr[k].style.backgroundColor = "#FFF";
                    arr[k].style.color = "#666";
                    (function(k){
                        arr[k].onmouseleave = function(){
                            arr[k].style.backgroundColor = "#FFF";
                            arr[k].style.color = "#666";
                        };
                    })(k)
                }
            }
        }
    };
    whole.Hale = obj;
})(window);